#!/bin/bash

#### 

### TO DO:
### more comands in menu, nmap scan

clear

DEFAULT_VIEVER=$(grep -e DEFAULT_VIEVER /etc/auto_ozint/global_config | cut -d"=" -f2)
DEFAULT_EDITOR=$(grep -e DEFAULT_EDITOR /etc/auto_ozint/global_config | cut -d"=" -f2)

VERSION=NaN

trap 'echo $(tput sgr 0); exit' SIGINT

MENU_MAIN_MESSAGE=1

## print logo:

echo ""



## check installed programs:

function check_bat {
	if [ -e /bin/bat ]; then
	BAT_CHECK=1
	else
	BAT_CHECK=0
	fi
}

function check_proxychains {
	if [ -e /bin/proxychains ]; then
	PROXYCHAINS_CHECK=1
	else
	PROXYCHAINS_CHECK=0
	fi
}

function check_nmap {
	if [ -e /bin/nmap ]; then
	NMAP_CHECK=1
	else
	NMAP_CHECK=0
	fi
}

function check_less {
	if [ -e /bin/less ]; then
	LESS_CHECK=1
	else
	LESS_CHECK=0
	fi
}

function check_masscan {
	if [ -e /bin/masscan ]; then
	MASSCAN_CHECK=1
	else
	MASSCAN_CHECK=0
	fi
}

function programs_checks {
	check_bat
	check_proxychains
	check_nmap
	check_less
	check_masscan
}

programs_checks

## warnings:
function warning_1 {
echo $(tput setaf 1)
echo 'Warning: this scan type create many files in scan directory'
echo $(tput sgr 0)
}

## Errors:

function invalid_worksapce_name {
	echo $(tput setaf 1)
	echo 'ERROR: invalid workspace name'
	echo $(tput sgr 0)
}

## Check root user
function check_root {
	if (( $EUID!=0 )); then
		echo $(tput setaf 1)
		echo 'WARNING!'
		echo 'You dont have root priveregies'
		echo $(tput sgr 0)
	fi
}
check_root

## check date
function check_date {
	DATE=$(date | sed 's. ._.g'| tr -s _)
}

check_date

echo "$(tput setaf 3)$DATE$(tput sgr 0)"
echo "$(tput setaf 3)Config in /etc/auto_ozint/global_config$(tput sgr 0)"
echo ' '	



## Check_Distro
function check_distro {
	DISTRO=$(grep -e NAME= /etc/os-release |head -n1| cut -d'=' -f2 | tr -d '"')
}

## Check_Distro_by_id
function check_distro_by_id {
	DISTRO_BY_ID=$(grep -e ID_LIKE= /etc/os-release | cut -d'=' -f2)
}

check_distro
check_distro_by_id

## Install program print tip

function install_program_print_tip {
	if [[ $DISTRO == "Arch Linux" ]]; then
		echo 'sudo pacman -S' $1
		else 
			if [[ $DISTRO_BY_ID == debian ]]; then
		    	echo 'sudo apt-get install' $1
		    else
		    	echo ''
		    fi
	fi
}


## check workspace name

function check_workspace_name {
	CHECK_WORKSPACE_NAME=1
	case $1 in

	  "lw" | "lsw" | "0")
		  CHECK_WORKSPACE_NAME=0
		  invalid_worksapce_name
	  ;;

	  *)
	 	 echo ''
	  ;;
		
	esac
}

## global menu


function menu_main_message {
	echo ''
	echo "$(tput setaf 6)you workspace is:$(tput sgr 0)"  $(tput setaf 5)$CURRENT_WORKSPACE$(tput sgr 0)
	echo ''
	echo "$(tput setaf 6)Enter the option (help for help, m for this message)$(tput sgr 0)"
	echo ''
	echo $(tput setaf 6)
	echo '1 - scan the ip (nmap)'
	echo ''
	echo $(tput sgr 0)
}


## worksapce exist check

function worksapce_exist_check {
	if [ -d /etc/auto_ozint/workspaces/$CURRENT_WORKSPACE ]; then
		echo ''
	else
		echo $(tput setaf 1)
		echo 'ERROR: worksapce  '$(tput setaf 5)$CURRENT_WORKSPACE$(tput sgr 0) $(tput setaf 1) 'not exist'
		echo $(tput sgr 0)
		menu		
	fi
}


## scan functions:

## 1 - scan the ip (nmap)

function choise_1 {
	if [ $NMAP_CHECK == 0 ]; then
		echo 'ERROR: nmap is not installed. Install now!'
		install_program_print_tip nmap
		menu
	fi
	worksapce_exist_check 
	echo ''
	echo $(tput setaf 6)
	echo 'Print type of scan:'
	echo ''
	echo '1 - all(Few scripts)', '2 - see nmap scripts page'
	echo ''
	echo $(tput sgr 0)
	read  SCANTYPE
	echo ''
	if [[ $SCANTYPE == "1" ]]; then
		warning_1
		if [[ $OUTPUT == "0" ]]; then
		echo  $(tput setaf 6)
			read -p 'Output directory (in workspace):' OUTPUT
		fi
		read -p 'Enter target:' TARGET
		echo $(tput sgr	0)
		
## scan type: all_standart

		check_date

		mkdir /etc/auto_ozint/workspaces/$CURRENT_WORKSPACE/$OUTPUT
		proxychains nmap $VERV_LEVEL -PR --spoof-mac 0 $TARGET  | tee -a /etc/auto_ozint/workspaces/$CURRENT_WORKSPACE/$OUTPUT/nmap_$TARGET_-PR_SCAN_$DATE
		proxychains nmap $VERV_LEVEL -O --spoof-mac 0 $TARGET  | tee -a /etc/auto_ozint/workspaces/$CURRENT_WORKSPACE/$OUTPUT/nmap_$TARGET_-O_SCAN_$DATE
	#	proxychains nmap $VERV_LEVEL -O --spoof-mac 0 $TARGET  | tee -a /etc/auto_ozint/workspaces/$CURRENT_WORKSPACE/$OUTPUT/nmap_$TARGET_-O_SCAN_$DATE
		choise_1
		else 
			echo $(tput setaf 1)
			echo 'ERROR: no option find'
			echo $(tput sgr 0)
			choise_1
	fi
}	

## help

function main_help_function {
	$DEFAULT_VIEVER /etc/auto_ozint/help 
}

## change workspace if current workspace deleted

function change_workspace_if_current_workspace_deleted {
	if [ $CURRENT_WORKSPACE == $RM_WORKSPACE ]; then
		unset RM_WORKSPACE
		choise_workspace
	fi
}

## delete workspace

function rmworkspace {
	echo $(tput setaf 6)
	read -p  "What workspace you remove? (lw for list): " RM_WORKSPACE
	echo $(tput sgr 0)
	if [[ $RM_WORKSPACE == "lw" || $RM_WORKSPACE == "lsw" ]]; then
		echo $(tput setaf 5)
		ls -t /etc/auto_ozint/workspaces/
		echo $(tput sgr 0)
	    rmworkspace
	fi
	if [ -d /etc/auto_ozint/workspaces/$RM_WORKSPACE ]; then
		echo $(tput setaf 6)
		read -p "Are you sure to delete workspace '$RM_WORKSPACE' ? (y or n) " ANS_RM_WORKSPACE
		echo $(tput sgr 0)
		if [[ $ANS_RM_WORKSPACE == y ]]; then
			rm -rf /etc/auto_ozint/workspaces/$RM_WORKSPACE
			if [[ -d /etc/auto_ozint/workspaces/$RM_WORKSPACE ]]; then
				echo $(tput setaf 1)
				echo 'ERROR: Workspace' $RM_WORKSPACE 'not deleted'
				echo $(tput sgr 0)
				unset RM_WORKSPACE
			else
				echo $(tput setaf 6)
				echo 'Workspace' $RM_WORKSPACE 'deleted'
				echo $(tput sgr 0)
				change_workspace_if_current_workspace_deleted
				unset RM_WORKSPACE
			fi
		else
			menu
		fi
	else
		echo $(tput setaf 1)
		echo 'ERROR: no workspace find'
		echo $(tput sgr 0)
		menu
	fi
}

## open file

function open_file {
	echo $(tput setaf 6)
	read -p 'Enter name of file: ' OPEN_FILE
	echo $(tput sgr 0)
	if [ -f /etc/auto_ozint/workspaces/$CURRENT_WORKSPACE/$OPEN_FILE ]; then
		$DEFAULT_EDITOR /etc/auto_ozint/workspaces/$CURRENT_WORKSPACE/$OPEN_FILE
	else
		echo /etc/auto_ozint/workspaces/$CURRENT_WORKSPACE/$OPEN_FILE
		echo $(tput setaf 1)
		echo 'ERROR: file not found'
		echo $(tput sgr 0)
	fi	
}

## see file 

function see_file {
	echo $(tput setaf 6)
	read -p 'Enter name of file: ' SEE_FILE
	echo $(tput sgr 0)
	if [ -f /etc/auto_ozint/workspaces/$CURRENT_WORKSPACE/$SEE_FILE ]; then
		$DEFAULT_VIEVER /etc/auto_ozint/workspaces/$CURRENT_WORKSPACE/$SEE_FILE
	else
		echo /etc/auto_ozint/workspaces/$CURRENT_WORKSPACE/$SEE_FILE
		echo $(tput setaf 1)
		echo 'ERROR: file not found'
		echo $(tput sgr 0)
	fi	
}

## global menu

function menu {

if [ $MENU_MAIN_MESSAGE == 1 ]; then
	menu_main_message
fi

echo ' '

read -p "$(tput setaf 6)Enter the option: $(tput sgr 0)" CHOISE

MENU_MAIN_MESSAGE=0

case $CHOISE in

	"1")
		choise_1
	;;	

	"help" | "h")
		main_help_function
		menu
	;;

	"lw" | "lsw")
			echo $(tput setaf 6)
			echo 'List of workspaces: '
			echo $(tput sgr 0)
			echo $(tput setaf 5)
			ls -t /etc/auto_ozint/workspaces/
			echo $(tput sgr 0)
			menu
	;;

	"l" | "ls")
			echo $(tput setaf 6)
			echo 'Files in' $CURRENT_WORKSPACE ': '
			echo $(tput sgr 0)
			echo $(tput setaf 5)
			ls -t /etc/auto_ozint/workspaces/$CURRENT_WORKSPACE
			echo $(tput sgr 0)
			menu
	;;

	"m" | "message")
			menu_main_message
			menu
	;;

	"rmw" | "rmworkspace")
		rmworkspace
		menu
	;;

	"version")
		echo $(tput setaf 3)
		echo 'Current version is: '$VERSION
		echo $(tput sgr 0)
		menu
	;;

	"recompilehelp")
		echo ' '
		create_help
		menu
	;;

	"conf")
		$DEFAULT_EDITOR /etc/auto_ozint/global_config
		menu
	;;

	"sconf")
		$DEFAULT_VIEVER /etc/auto_ozint/global_config
		menu
	;;

	"p" | "printw")
		echo $(tput setaf 5)
		echo $CURRENT_WORKSPACE
		echo $(tput sgr 0)
		menu
	;;

	"pwd")
		echo $(tput setaf 6)
		echo /etc/auto_ozint/workspaces/$(tput setaf 5)$CURRENT_WORKSPACE
		echo $(tput sgr 0)
		menu
	;;

	"mv" | "move")
		echo ' '
		choise_workspace
	;;

	"s" | "see")
		see_file
		menu
	;;

	"o" | "open")
		open_file
		menu
	;;

	*) 
		echo $(tput setaf 1)
		echo 'ERROR: no option find'
		echo $(tput sgr 0)
		menu
	;;
	

esac

}


OUTPUT=0
CURRENT_WORKSPACE=0
VERV_LEVEL=0
ARG1=$1

## create:

## create workspace

function create_workspace {
	echo ' '
	echo $(tput setaf 6)
	read -p "enter name of NEW workspace: $(tput sgr 0)" NEW_WORKSPACE_NAME
	if [[ -d /etc/auto_ozint/workspaces/$NEW_WORKSPACE_NAME ]]; then
		echo $(tput setaf 1)
		echo 'ERROR: workspace already exist'	
		echo $(tput sgr 0)
		CURRENT_WORKSPACE=$NEW_WORKSPACE_NAME
		menu
	else
		 check_workspace_name $NEW_WORKSPACE_NAME
		 if [ $CHECK_WORKSPACE_NAME == 1 ]; then
			 mkdir /etc/auto_ozint/workspaces/$NEW_WORKSPACE_NAME
			 echo ' '
			 if [ -d /etc/auto_ozint/workspaces/$NEW_WORKSPACE_NAME ]; then
	  	 		 echo $(tput setaf 6) 'workspace ' $(tput sgr 0) $(tput setaf 5) $NEW_WORKSPACE_NAME $(tput sgr 0) $(tput setaf 6)  ' created' $(tput sgr 0)

				 CURRENT_WORKSPACE=$NEW_WORKSPACE_NAME

				 unset NEW_WORKSPACE_NAME
				 menu
			 else
				echo $(tput setaf 1)
	 			echo 'ERROR: workspace not created'	
	    		echo $(tput sgr 0)
	    		choise_workspace
	    		menu
	      
	    	 fi
	     else
	     	if [ -d /etc/auto_ozint/workspaces/$CURRENT_WORKSPACE ]; then
	    	 	menu
	     	else
	        	create_workspace
	        fi
		 fi
	fi
}

## choise workspace:
function choise_workspace {
	read -p "$(tput setaf 6)Move to workspace! (enter name of worksapce; lw to list of workspaces): $(tput sgr 0)" CURRENT_WORKSPACE
		if [[ $CURRENT_WORKSPACE == 'lw' || $CURRENT_WORKSPACE == 'lsw' ]]; then
			echo $(tput setaf 5)
			ls -t /etc/auto_ozint/workspaces
			echo $(tput sgr 0)
			choise_workspace
		else
			if [  -d /etc/auto_ozint/workspaces/$CURRENT_WORKSPACE ]; then
				echo ' '
				echo $(tput setaf 6)
				echo 'Welcome to workspace' $(tput setaf 5) $CURRENT_WORKSPACE $(tput setaf 0)
				menu
			else
				echo $(tput setaf 1)
				echo 'Error: no workspace found, create this? (Enter y or n)'
				echo $(tput sgr 0)
				read YES_NO
				if [ $YES_NO == "y" ]; then
					unset YES_NO
					create_workspace
					menu
				else
					unset YES_NO
					CURRENT_WORKSPACE=0
					choise_workspace
					menu
				fi
			fi
		fi
}

# checks:

## check workspace:
function check_workspace {
	if [ -d /etc/auto_ozint/workspaces ]; then
		if [[ -z $(echo $(ls /etc/auto_ozint/workspaces/)) ]]; then
		echo $(tput setaf 6)
		echo 'No workspace found, create this!'
		echo $(tput sgr 0)
		create_workspace
		else
			choise_workspace
		fi
	else
		mkdir /etc/auto_ozint/workspaces
		echo /etc/auto_ozint/workspaces/ created
		check_workspace
	fi
} 

## create help

function create_help {
	touch /etc/auto_ozint/help
	echo "/etc/auto_ozint/help created"
	echo $(tput setaf 6)
	cat > /etc/auto_ozint/help <(echo "
Options in menu:
	Main:
	 
	help or h - get this message
	m or message - print standart menu message
	
	Workspaces:
	
	lw or lsw - see avalable workspaces
	rmw or rmworksapce - delete workspace
	pwd - print workspace location in filesystem
	mv or move - change workspace
	p or printw - print current workspace

	Files:

	l or ls - see files in workspace
	rm - delete files in workspace
	s or see - open files in workspace witch default viever
	o or open - open files in workspace witch default editor

	Other:
	
	conf - open config file (in /etc/auto_ozint/global_config)
	sconf - see config file (in /etc/auto_ozint/global_config)
	version - print version
	recompilehelp - update help in /etc/auto_ozint/help
			 ")
	echo $(tput sgr 0)
}

## Check help

function check_help {
	if [ -f /etc/auto_ozint/help ]; then
		check_global_config
	else
	    create_help
		check_global_config
	fi
}

## Create config 1
function create_default_config_1 {
	touch /etc/auto_ozint/global_config
	echo '/etc/auto_ozint/global_config created'
	cat >> /etc/auto_ozint/global_config <(echo "
BACKUP_WORKSPACES=true
	")
}

## Create config 2
function create_default_config_2 {
	if [ $BAT_CHECK == 1 ]; then
		DEFAULT_VIEVER=/bin/bat
	else
		if [ $LESS_CHECK == 1 ]; then
			DEFAULT_VIEVER=/bin/less
		else
			DEFAULT_VIEVER=/bin/more
		fi
	fi
	
	cat >> /etc/auto_ozint/global_config <(echo 'DEFAULT_VIEVER='$DEFAULT_VIEVER)
	cat >> /etc/auto_ozint/global_config <(echo 'DEFAULT_EDITOR='/bin/nano)

}

## Check global config
function check_global_config {
	if [ -f /etc/auto_ozint/global_config ]; then
		check_workspace
	else
		create_default_config_1
		create_default_config_2
		check_workspace
	fi
}

## Check directory
function check_directory {
	if [ -e /etc/auto_ozint/ ]; then
		check_help
		check_global_config
		check_workspace
	else
		mkdir /etc/auto_ozint/
		echo /etc/auto_ozint/ created
		check_directory
	fi
}

check_directory

menu
menu
menu
menu
menu






